package com.vercon.moensmart.utils

import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import com.vercon.moensmart.MainApplication.Companion.getContext
import com.vercon.moensmart.R

class ToastUtils {


    companion object{
        private var toast: Toast? = null
        fun showToast(msg: String?) {
            if (toast != null) {
                toast!!.cancel()
                toast = null
            }
            if (getContext().resources == null) {
                return
            }
            try {
                toast = Toast(getContext())
                toast!!.duration = Toast.LENGTH_LONG
                toast!!.setGravity(Gravity.TOP, 0, 0)
                val inflater = LayoutInflater.from(getContext())
                val toastLayout = inflater.inflate(R.layout.layout_toast, null) as LinearLayout
                val txtToast = toastLayout.findViewById<TextView>(R.id.tv_toast)
                txtToast.text = msg
                toast!!.view = toastLayout
                toast!!.show()
            } catch (e: WindowManager.BadTokenException) {
                e.printStackTrace()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

}